<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="iconfont arr_left" @click="toLeft">&#xe6ef;</span>
    <span class="iconfont arr_right" @click="toRight">&#xe6ed;</span>
    <span class="cat_name">{{ catTitle }} 销售占比率</span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allData: null,
      chartInstance: null,
      //   左右轮播
      currentIndex: 0,
    }
  },
  mounted() {
    this.chartInit()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  computed: {
    catTitle() {
      if (!this.allData) {
        return ''
      }
      return this.allData[this.currentIndex].name
    },
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    chartInit() {
      this.chartInstance = this.$echarts.init(this.$refs.hot_ref, 'chalk')
      const initOption = {
        title: {
          text: '热销商品销售金额占比',
          left: 20,
          top: 20,
        },
      }
      this.chartInstance.setOption(initOption)
    },
    async getData() {
      const {data: res} = await this.$http.get('/hotproduct')
      this.allData = res
      this.updateChart()
    },
    updateChart() {
      // 处理图表需要的数据
      const pieData = this.allData[this.currentIndex].children.map((item) => {
        return {
          value: item.value,
          name: item.name,
          children: item.children,
        }
      })
      const legendData = this.allData[this.currentIndex].children.map(
          (el) => el.name
      )
      const dataOption = {
        legend: {
          data: legendData,
          top: '5%',
          icon: 'circle',
        },
        tooltip: {
          trigger: 'item',
          formatter(arg) {
            let tipArray = []
            let totalPrecen = ` 总占比&nbsp;&nbsp;&nbsp;${arg.percent}%`
            tipArray.push(totalPrecen)
            arg.data.children.map( (item,index)=> {
              let childStr = `
              ${item.name}&nbsp;&nbsp;&nbsp;
              ${parseInt((item.value / arg.value) * 100) + '%'}
`
              tipArray.push(childStr)
            })

            return tipArray.join('<br/>')

          }
        },
        series: [
          {
            data: pieData,
            type: 'pie',
            radius: ['30%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#222733',
              borderWidth: 2,
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold',
              },
            },
            label: {
              // 隐藏文字
              show: false,
              position: 'center',
              fontSize: '40',
            },
            labelLine: {
              // 隐藏线
              show: false,
            },
            emphasis: {
              label: {
                // 高亮显示文字
                show: true,
              },
            },
          },
        ],
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.hot_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize / 2,
          },
        },
        legend: {
          textStyle: {
            fontSize: titleFontSize / 2.3,
          },
        },
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    toLeft() {
      this.currentIndex--
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1
      }
      this.updateChart()
    },
    toRight() {
      this.currentIndex++
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0
      }
      this.updateChart()
    },
  },
}
</script>

<style lang="less" scoped>
.arr_left {
  position: absolute;
  font-size: 30px;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}

.arr_right {
  position: absolute;
  font-size: 30px;
  right: 10%;
  top: 50%;
  color: white;
  transform: translateY(-50%);
  cursor: pointer;
}

.cat_name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  font-weight: bold;
  color: white;
}
</style>
